﻿Public Class ApplyCustomPatternsForm

    Dim ApplyToBudget As Boolean = False

    Private Sub ApplyCustomPatternsForm_Shown(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown

        ApproveButton.Visible = False
        DeclineButton.Visible = False
        ApplyButton.Visible = True
        ReadyLabel.Visible = False


        TheFilter.zShowButtons = True
        TheFilter.zDataView = LoadDataView("select *, ' ' as Checked from dbo.View_Provider_Filter_Options")

        BindRepeater()

        ApplyPatternToAllocationLineToolStripMenuItem.Text = "Apply pattern to " & DirectCast(BudgetsFormV2.AllocationBox.DataSource, DataView)(BudgetsFormV2.AllocationBox.SelectedIndex)("Allocation_Description")
        ApplyPatternToBudgetLineToolStripMenuItem.Text = "Apply pattern to budget " & DirectCast(BudgetsFormV2.BudgetBox.DataSource, DataView)(BudgetsFormV2.BudgetBox.SelectedIndex)("Budget_Description")
        ApplyToBudget = False

    End Sub

    Private Sub BindRepeater()


        'ProviderLabel.DataBindings.Clear()
        'ProviderLabel.DataBindings.Add(New System.Windows.Forms.Binding("Text", TheFilter.zDataView, "Provider Name"))

        'CheckLabel.DataBindings.Clear()
        'CheckLabel.DataBindings.Add(New System.Windows.Forms.Binding("Text", TheFilter.zDataView, "Checked"))

        'UPINLabel.DataBindings.Clear()
        'UPINLabel.DataBindings.Add(New System.Windows.Forms.Binding("Text", TheFilter.zDataView, "UPIN"))

        'ProvidersRepeater.DataSource = TheFilter.zDataView

        ProvidersRepeater.ItemCount = TheFilter.zDataView.Count

    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackButton.Click
        Me.Close()

    End Sub

    Private Sub ApplyPatternToAllocationLineToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApplyPatternToAllocationLineToolStripMenuItem.Click
        ApplyButton.Visible = False
        ApproveButton.Visible = True
        DeclineButton.Visible = True

        TheFilter.zDataView.RowFilter = "Checked = 'X'"
        TheFilter.Enabled = False
        ApplyToBudget = False
        ReadyLabel.Text = "Click Approve to apply to Allocation Line"
        ReadyLabel.Visible = True

        ProvidersRepeater.ItemCount = TheFilter.zDataView.Count


        ' MessageBox.Show("Please confirm by clicking the Approve button", "Please approve", MessageBoxButtons.OK, MessageBoxIcon.Information)

    End Sub

    Private Sub ApplyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApplyButton.Click
        '  ApplyButton.Visible = False

    End Sub

    Private Sub ApproveButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApproveButton.Click

        If ReadyLabel.Text.Contains("Budget") Then
            ApplyPatternforAllocation(True)
        Else
            ApplyPatternforAllocation(False)
        End If


    End Sub

    Private Sub DeclineButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeclineButton.Click
        TheFilter.Enabled = True
        ApproveButton.Visible = False
        DeclineButton.Visible = False

        ApplyButton.Visible = True
        ReadyLabel.Visible = False

        TheFilter.ApplyFilter()
    End Sub

    Private Sub ApplyPatternToBudgetLineToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ApplyPatternToBudgetLineToolStripMenuItem.Click
        ApplyButton.Visible = False
        ApproveButton.Visible = True
        DeclineButton.Visible = True

        TheFilter.zDataView.RowFilter = "Checked = 'X'"
        TheFilter.Enabled = False
        ApplyToBudget = False
        ReadyLabel.Text = "Click Approve to apply to all Allocation Lines in the current Budget"
        ReadyLabel.Visible = True

        ProvidersRepeater.ItemCount = TheFilter.zDataView.Count

        '  MessageBox.Show("Please confirm by clicking the Approve button", "Please approve", MessageBoxButtons.OK, MessageBoxIcon.Information)
    End Sub

    Private Sub ApplyPatternforAllocation(ByVal Budget As Boolean)

        'do it as a backgroud worker

        'first delete all provider patterns for all selected providers
        'create RID list

        Dim UPIN As String = ""
        Dim RidList As New List(Of String)

        RidList.Clear()

        Dim sql As String

        Dim r As DataRowView
        Dim DV As DataView

        For Each r In TheFilter.zDataView

            If UPIN = "" Then
                UPIN = r("UPIN")
            Else
                UPIN = UPIN & ", " & r("UPIN")
            End If

            sql = "select RID from all_provider_Fields where UPIN = " & r("UPIN") & " and lead = 1"

            DV = LoadDataView(sql)

            If DV.Count <> 0 Then
                RidList.Add(DV(0)("RID"))
            End If

        Next
        Dim params As New List(Of Object)

        'insert provider patterns based on the custom pattern for all selected providers
        With BudgetsFormV2

            If Budget = False Then
                sql = "delete from provider_patterns where period_id = " & .PeriodBox.zSelectedPeriod_ID & " and allocation_ID = '" & .AllocationBox.SelectedValue & "' and RID in (select RID from all_provider_Fields where UPIN in (" & UPIN & ") and Lead = 1)"
                RunNonQuery(sql)

                sql = "insert into Provider_patterns select " & .PeriodBox.zSelectedPeriod_ID & " as Period_ID, '" & .AllocationBox.SelectedValue & "' as Allocation_ID, a.RID, Month, Percentage  " & _
                        "from Patterns p, (Select RID from All_Provider_Fields where UPIN in (" & UPIN & ") and Lead = 1) a " & _
                        "where Pattern_ID = '" & .CustomPatternBox.SelectedValue & "'"


                RunNonQuery(sql)

                params.Clear()

                params.Add(.PeriodBox.zSelectedPeriod_ID) 'period_ID
                params.Add(.AllocationBox.SelectedValue) ' allocation id
                params.Add(RidList) ' RIDS

                'apply the active payments procedure to all selected providers
                DoSomeWork("UpdateActivePayments", params, False)
            Else
                sql = "delete from provider_patterns where period_id = " & .PeriodBox.zSelectedPeriod_ID & " and allocation_ID in (select allocation_ID from Allocation_Lines where Budget_ID = '" & .BudgetBox.SelectedValue & "') and RID in (select RID from all_provider_Fields where UPIN in (" & UPIN & ") and Lead = 1)"
                RunNonQuery(sql)

                Dim alids As DataView
                Dim rec As DataRowView

                alids = LoadDataView("select Allocation_ID from Allocation_lines where Budget_ID = '" & .BudgetBox.SelectedValue & "'")

                For Each rec In alids
                    sql = "insert into Provider_patterns select " & .PeriodBox.zSelectedPeriod_ID & " as Period_ID, '" & rec("Allocation_ID") & "' as Allocation_ID, a.RID, Month, Percentage  " & _
                        "from Patterns p, (Select RID from All_Provider_Fields where UPIN in (" & UPIN & ") and Lead = 1) a " & _
                        "where Pattern_ID = '" & .CustomPatternBox.SelectedValue & "'"
                    RunNonQuery(sql)
                Next

                params.Clear()

                params.Add(.PeriodBox.zSelectedPeriod_ID) 'period_ID
                params.Add(.BudgetBox.SelectedValue) ' Budget_ID
                params.Add(RidList) ' RIDS

                'apply the active payments procedure to all selected providers
                DoSomeWork("UpdateActivePaymentsBudget", params, False)
            End If
            

        End With

        

        MessageBox.Show("The pattern has been applied to the selected providers", "Pattern Applied", MessageBoxButtons.OK, MessageBoxIcon.Information)
        ReadyLabel.Visible = False
        DeclineButton.Visible = False
        ApproveButton.Visible = False
        ApplyButton.Visible = True

        BudgetsFormV2.LoadAndBindProvPatterns()

    End Sub

    Private Sub UPINToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles UPINToolStripMenuItem.Click

        DirectCast(ProvidersRepeater.DataSource, DataView).Sort = "UPIN ASC"

    End Sub

    Private Sub ProviderNameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ProviderNameToolStripMenuItem.Click
        DirectCast(ProvidersRepeater.DataSource, DataView).Sort = "[Provider Name] ASC"
    End Sub

    Private Sub CheckedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CheckedToolStripMenuItem.Click
        DirectCast(ProvidersRepeater.DataSource, DataView).Sort = "Checked DESC"
    End Sub

    Private Sub ToolStripSplitButton1_ButtonClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripSplitButton1.ButtonClick
        DirectCast(ProvidersRepeater.DataSource, DataView).Sort = "UPIN ASC"
    End Sub

    Private Sub ToolStripButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click


        Dim r As DataRowView

        For Each r In TheFilter.zDataView

            r("Checked") = "X"

        Next

        ProvidersRepeater.BeginResetItemTemplate()
        ProvidersRepeater.EndResetItemTemplate()


    End Sub

    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Dim r As DataRowView

        For Each r In TheFilter.zDataView

            r("Checked") = " "

        Next

        ProvidersRepeater.BeginResetItemTemplate()
        ProvidersRepeater.EndResetItemTemplate()

    End Sub

    Private Sub TheCheckClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Dim l As Label

        l = sender

        If l.Text = "X" Then
            l.Text = " "
        Else
            l.Text = "X"
        End If

    End Sub


    Private Sub ProvidersRepeater_DrawItem(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemEventArgs) Handles ProvidersRepeater.DrawItem
        Dim panel As FlowLayoutPanel

        panel = TryCast(e.DataRepeaterItem.Controls("TheFlowPanel"), FlowLayoutPanel)

        If Not panel Is Nothing Then

            Dim l As Label

            l = TryCast(panel.Controls("CheckLabel"), Label)


            RemoveHandler l.Click, AddressOf TheCheckClick
            AddHandler l.Click, AddressOf TheCheckClick

        End If

    End Sub

   
    Private Sub ApplyCustomPatternsForm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        ProvidersRepeater.VirtualMode = True
    End Sub

    Private Sub ProvidersRepeater_ItemValueNeeded(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemValueEventArgs) Handles ProvidersRepeater.ItemValueNeeded
        Dim i As Integer = e.ItemIndex

        If (i >= 0 AndAlso i < TheFilter.zDataView.Count) Then


            Select Case e.Control.Name

                Case "ProviderLabel"
                    e.Value = TheFilter.zDataView(i)("UPIN") & " - " & TheFilter.zDataView(i)("Provider Name")
                Case "CheckLabel"
                    e.Value = TheFilter.zDataView(i)("Checked")

            End Select


        End If

    End Sub

    Private Sub ProvidersRepeater_ItemValuePushed(ByVal sender As System.Object, ByVal e As Microsoft.VisualBasic.PowerPacks.DataRepeaterItemValueEventArgs) Handles ProvidersRepeater.ItemValuePushed
        Dim i As Integer = e.ItemIndex

        If (i >= 0) Then


            Select Case e.Control.Name

                Case "CheckLabel"
                    TheFilter.zDataView(i)("Checked") = e.Value

            End Select


        End If
    End Sub

    Private Sub OnFiltered(ByVal sender As Object, ByVal e As EventArgs) Handles TheFilter.Filtered

        ProvidersRepeater.ItemCount = TheFilter.zDataView.Count

    End Sub

    Private Sub OnFilterCleared(ByVal sender As Object, ByVal e As EventArgs) Handles TheFilter.FilterCleared

        ProvidersRepeater.ItemCount = TheFilter.zDataView.Count
    End Sub

End Class